package pro.cherish.knowledgeutil.json;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import pro.cherish.knowledgeutil.column.ColumnUtilExt;
import pro.cherish.knowledgeutil.map.MapUtilsExt;
import pro.cherish.knowledgeutil.str.StringUtilExt;

import java.util.Map;

/**
 * @author 创建人：cherish
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2019-11-01 cherish 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：JsonUtilExt
 * @Description 功能说明：
 * <p>
 * TODO
 * </p>
 * **********************************************************************
 * @date 创建日期： 2019-11-01 cherish
 */
@Slf4j
public class JsonUtilExt {

    /**
     * 实体转换成json字符串
     *
     * @param obj
     * @return
     */
    public static String toJsonString(Object obj) {
        return JSON.toJSONString(obj);
    }

    /**
     * json字符串转实体
     *
     * @param jsonString
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T fromJson(String jsonString, Class<T> clazz) {
        return JSON.parseObject(jsonString, clazz);
    }

    /**
     * 打印JSON格式对象
     *
     * @param obj 源对象
     */
    public static void printObject(Object obj) {
        printObject(obj, true);
    }


    /**
     * 将对象以json格式输出
     *
     * @param obj            源对象
     * @param isPrettyFormat 是否完美格式化
     */
    public static void printObject(Object obj, boolean isPrettyFormat) {
        String json;
        if (isPrettyFormat) {
            json = JSONObject.toJSONString(obj,
                    new SerializerFeature[]{SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
                            SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullBooleanAsFalse,
                            SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullNumberAsZero});
        } else {
            json = JSONObject.toJSONString(obj,
                    new SerializerFeature[]{SerializerFeature.WriteMapNullValue,
                            SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullBooleanAsFalse,
                            SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullNumberAsZero});
        }
        log.info(json);
    }

    /**
     * 将对象以json格式打印，并包含前缀字符串
     *
     * @param prefixStr 前缀字符串
     * @param obj       源对象
     */
    public static void printObject(String prefixStr, Object obj) {
        log.info(String.format(prefixStr + "%s", toJsonString(obj)));
    }

    /**
     * json字符串转为map
     *
     * @param json  源json字符串
     * @param camel 是否键驼峰的标识
     * @return
     */
    public static Map<String, Object> json2Map(String json, boolean camel) {
        Map map = JSON.parseObject(json, Map.class);
        Map<String, Object> resultMap = MapUtilsExt.newHashMap();
        for (Object key : map.keySet()) {
            if (camel) {
                resultMap.put(ColumnUtilExt.underlineToCamel(StringUtilExt.toString(key)), map.get(key));
            } else {
                resultMap.put(StringUtilExt.toString(key), map.get(key));
            }
        }
        return resultMap;
    }

    public static Map<String, Object> json2Map(String json) {
        return json2Map(json, false);
    }

}
